package com.wang.exam.controller;

import com.wang.exam.pojo.ExamCourses;
import com.wang.exam.pojo.Teacher;
import com.wang.exam.service.CourseService;
import com.wang.exam.service.loginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ExamCourseController {
    public String major_key;
    @Autowired
    CourseService courseService;
    @RequestMapping("/courseList")
    @ResponseBody                                           //向浏览器返回json数据
    public Map<String,Object> courseList(int page , int rows){
        Map<String,Object> map  = new HashMap<>();
        int n = courseService.getCCount();
        List<ExamCourses> teacherList = courseService.getCourseList(page,rows);
        map.put("rows",teacherList);
        map.put("total",n);
        return map;
    }
    @RequestMapping("/deleteCourses")
    @ResponseBody
    public Object deleteCourses(@RequestBody String ids){
        String str = ids.substring(1,ids.length()-1);
        String[] strings = str.split("\\.");
        courseService.deleteId(strings);
        Map<String,String> map = new HashMap<String,String>();
        map.put("code","200");
        map.put("msg","批量删除成功！");
        return map;
    }
    @RequestMapping("/add_course_log")
    @ResponseBody
    public Object addCourse(
            @RequestParam("teacher_name")String teacher_name,
            @RequestParam("major_id")Integer major_id,
            @RequestParam("course_id")Integer course_id,
            @RequestParam("teacher_id")Integer  teacher_id
    ){
        ExamCourses examCourses = new ExamCourses(teacher_name,major_id,course_id,teacher_id);
        Map<String,String> map = new HashMap<String,String>();
        try{
            courseService.addCourse(examCourses);
            map.put("code","200");
            map.put("msg","添加成功！");
            return map;
        }catch (Exception e){
            if(e.getCause() instanceof java.sql.SQLIntegrityConstraintViolationException){   //判断异常类型
                map.put("msg","添加失败");
                return map;
            }else{
                map.put("msg","添加失败,未知类型错误！");
                return map;
            }

        }
    }
    @RequestMapping("/up_course_log")
    @ResponseBody
    public Object upCourse(@RequestParam("major_id")Integer major_id,
                           @RequestParam("course_id")Integer course_id,
                           @RequestParam("teacher_id")Integer  teacher_id,
                           @RequestParam("teacher_name")String  teacher_name){

        Map<String,String> map = new HashMap<String,String>();
        Map<String,Object> courseMap  = new HashMap<String,Object>();
        courseMap.put("teacher_name",teacher_name);
        courseMap.put("major_id",major_id);
        courseMap.put("teacher_id",teacher_id);
        courseMap.put("course_id",course_id);
        try{
            courseService.updateCourse(courseMap);
            map.put("code","200");
            map.put("msg","修改成功！");
            return map;
        }catch (Exception e){
            System.out.println(e);
            if(e.getCause() instanceof java.sql.SQLIntegrityConstraintViolationException){   //判断异常类型
                map.put("msg","修改失败,考试不存在！");
                return map;
            }else{
                map.put("msg","修改失败,未知类型错误！");
                return map;
            }

        }

    }
    @RequestMapping("/de_course_log")
    @ResponseBody
    public Object deCourse(@RequestParam("major_id")Integer major_id,
                           @RequestParam("course_id")Integer course_id,
                           @RequestParam("teacher_id")Integer  teacher_id){
        Map<String,String> map = new HashMap<String,String>();
        Map<String,Object> courseMap  = new HashMap<String,Object>();

        try{
            courseService.deleteCourse(courseMap);
            map.put("code","200");
            map.put("msg","删除成功！");
            return map;
        }catch (Exception e){
            if(e.getCause() instanceof java.sql.SQLIntegrityConstraintViolationException){   //判断异常类型
                map.put("msg","删除失败,教师id不存在！");
                return map;
            }else{
                map.put("msg","删除失败,未知类型错误！");
                return map;
            }

        }
    }
    @RequestMapping("/keyCourses")                                     //获取前端传回的值，并且解码为字符串
    @ResponseBody
    public Object keywordCourses(@RequestBody String keyword) throws UnsupportedEncodingException {
        this.major_key = keyword;
        return "123";
    }

    @RequestMapping("/keyCourseList")
    @ResponseBody
    public Object keyCourses(String keyword){                            //前端传回来的值进行模糊查询，返回查询结果
        keyword = this.major_key;
        int l = keyword.length();
        List<ExamCourses> examCourses = courseService.getCourse(keyword.substring(0,l-1));
        return examCourses ;
    }
}
